// Copyright (c) 2006, Gregory Todd <greg@oddsim.com>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
// 
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Othello.Engine;

namespace Othello.WinOthello
{
    public partial class WinOthello : Form
    {
        NewGame dialog = new NewGame();
        Game game = new Game();

        bool moveHint;

        public WinOthello()
        {
            InitializeComponent();
        }

        private void StartGame()
        {
            moveHint = false;

            Player blackPlayer;
            Player whitePlayer;

            if (dialog.BlackComputer)
                blackPlayer = new Player(dialog.BlackDifficulty);
            else
                blackPlayer = new Player();


            if (dialog.WhiteComputer)
                whitePlayer = new Player(dialog.WhiteDifficulty);
            else
                whitePlayer = new Player();


            game.Start(whitePlayer, blackPlayer);

            DrawBoard();

            Prompt();
        }

        private void Prompt()
        {
            if (game.TurnNone)
            {
                if (game.PointsBlack > game.PointsWhite)
                    MessageBox.Show(this, "Black wins");
                if (game.PointsBlack < game.PointsWhite)
                    MessageBox.Show(this, "White wins");
                if (game.PointsBlack == game.PointsWhite)
                    MessageBox.Show(this, "The game is a tie");
            }
            if (game.TurnComputer)
                computerTimer.Start();

            if (game.TurnHuman)
                btnHint.Visible = true;
            else
                btnHint.Visible = false;
        }

        private void DrawBoard()
        {
            if (game.TurnBlack)
                lblPlayer.Text = "Black";
            else if (game.TurnWhite)
                lblPlayer.Text = "White";
            else
                lblPlayer.Text = "None";

            lblDark.Text = game.PointsBlack.ToString();
            lblLight.Text = game.PointsWhite.ToString();

            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    SpaceButton button = boardTable1.board[x, y];

                    if (game.IsSpaceWhite(x, y))
                    {
                        if (button.isDark())
                            button.Image = Properties.Resources.Split;
                        else if (button.isBlank())
                            button.Image = Properties.Resources.Light;
                        button.setLight();
                    }
                    else if (game.IsSpaceBlack(x, y))
                    {
                        if (button.isLight())
                            button.Image = Properties.Resources.Split;
                        else if (button.isBlank())
                            button.Image = Properties.Resources.Dark;
                        button.setDark();
                    }
                    else if (moveHint && game.IsValid(x, y))
                    {
                        button.Image = Properties.Resources.Open;
                        button.setBlank();
                    }
                   else
                    {
                        button.Image = Properties.Resources.Blank;
                        button.setBlank();
                    }
                }
            }
            drawTimer.Start();
        }

        void boardTable1_SelectEvent(object sender, SelectArgs e)
        {
            if (game.TurnHuman && game.IsValid(e.X, e.Y))
            {
                moveHint = false;
                game.PlayHuman(e.X, e.Y);
                Prompt();
                DrawBoard();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            computerTimer.Stop();
            if (game.TurnComputer)
            {
                progressBar1.Visible = true;
                newGameToolStripMenuItem.Enabled = false;
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            computerTimer.Stop();
            DialogResult result = dialog.ShowDialog(this);
            if (result == DialogResult.OK)
                StartGame();
            else
                Prompt();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            game.PlayComputer();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            DrawBoard();
            Prompt();
            progressBar1.Visible = false;
            newGameToolStripMenuItem.Enabled = true;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void drawTimer_Tick(object sender, EventArgs e)
        {
            drawTimer.Stop();
            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    SpaceButton button = boardTable1.board[x, y];

                    if (game.IsSpaceWhite(x, y))
                    {
                        button.Image = Properties.Resources.Light;
                        button.setLight();
                    }
                    else if (game.IsSpaceBlack(x, y))
                    {
                        button.Image = Properties.Resources.Dark;
                        button.setDark();
                    }
                }
            }

        }

        private void btnHint_Click(object sender, EventArgs e)
        {
            moveHint = !moveHint;
            DrawBoard();
        }

        private void gameTreeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Player.treeNode != null)
            {
                GameTree gameTree = new GameTree(Player.treeNode.FirstNode);
                gameTree.Show(this);
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AboutBox1().ShowDialog(this);
        }
    }
}